/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gad.ui;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.Collections;
import java.util.LinkedList;
import javax.swing.JComponent;
import org.netbeans.api.visual.border.BorderFactory;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author Tomek
 */
public class BoxScene extends Scene
{

    private LayerWidget _connectionLayer;
    private LayerWidget _mainLayer;
    private LinkedList<DropAreaWidget> _dropAreaWidgets = new LinkedList<DropAreaWidget>();


    public BoxScene()
    {

        _mainLayer = new LayerWidget(this);
        _mainLayer.setBorder(BorderFactory.createEmptyBorder(20));
        _connectionLayer = new LayerWidget(this);
        //_dropAreaLayer.setVisible(true);

        this.addChild(_mainLayer);

        this.addChild(_connectionLayer);
    //this.addChild( _dropAreaLayer);
    }

//    public LayerWidget getDropAreaLayer()
//    {
//        return _dropAreaLayer;
//    }
    public void addDropAreaWidget(DropAreaWidget widget)
    {
        _dropAreaWidgets.add(widget);
    }

    public void setDropAreasVisible(boolean visible)
    {
        for (DropAreaWidget w : _dropAreaWidgets)
        {
            w.setDropAreaVisible(visible);
        }
    }

    public LayerWidget getConnectionLayer()
    {
        //return _mainLayer;
        return _connectionLayer;
    }

    public LayerWidget getMainLayer()
    {
        return _mainLayer;
    }

    public Iterable<DropAreaWidget> getDropAreas()
    {
        return Collections.unmodifiableCollection(_dropAreaWidgets);
    }
    

    public DropAreaWidget FindBestDropArea(Widget dragged, Point arg2)
    {
        //int maxDist = Integer.MAX_VALUE;
        DropAreaWidget bestDropArea = null;

        Point p = dragged.convertLocalToScene(arg2);

        for (DropAreaWidget w : _dropAreaWidgets)
        {
            Rectangle r = w.convertLocalToScene(w.getBounds());
            r.grow(10, 10);
            if (r.contains(p))
            {
                bestDropArea = w;
//            if (maxDist > p.distance(w.convertLocalToScene(w.getLocation())))
//            {
//                maxDist = (int) p.distance(w.convertLocalToScene(w.getLocation()));
//                bestDropArea = w;
//            }
            }
        }

        turnHighlightOff();
        if (bestDropArea != null)
        {

            bestDropArea.setHighlight(true);
            //System.out.println("HIT!!!");
        //System.out.print( "FBDA " + arg2.toString() + ", " + bestDropArea.
        //    convertLocalToScene(bestDropArea.getLocation()) + ", ");
        }
        //System.out.println(
        //        "Best drop area for " + arg2.toString() + " point is " + bestDropArea);
        return bestDropArea;
    }

    private void turnHighlightOff()
    {
        for (DropAreaWidget w : _dropAreaWidgets)
        {
            w.setHighlight(false);
        }
    }

    public void clear()
    {
        _mainLayer.removeChildren();
        _connectionLayer.removeChildren();
        _dropAreaWidgets.clear();
    }
}
